﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>班级信息管理</title>
  <!-- 引入Bootstrap CSS -->
  <link href="${pageContext.request.contextPath}/resources/bootstrap5/css/bootstrap.min.css" rel="stylesheet">
  <!-- 使用国内稳定的BootCDN Bootstrap Icons -->
  <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap-icons/1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
  <!-- 备用CDN链接 -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">
  <style>

    .header .header-user img[src*="baimao.png"],
    .header .header-user .header-user-avatar,
    .header .header-user #userAvatar,
    .header-user-avatar,
    #userAvatar {
      width: 32px !important;
      height: 32px !important;
      max-width: 32px !important;
      max-height: 32px !important;
      min-width: 32px !important;
      min-height: 32px !important;
      border-radius: 50% !important;
      margin-right: 8px !important;
      object-fit: cover !important;
      background: #fff !important;
      cursor: pointer !important;
      transition: transform 0.2s !important;
      border: 2px solid rgba(255,255,255,0.3) !important;
      display: inline-block !important;
      vertical-align: middle !important;
      box-sizing: border-box !important;
    }
    
    /* 悬停效果 */
    .header .header-user img[src*="baimao.png"]:hover,
    .header .header-user .header-user-avatar:hover,
    .header .header-user #userAvatar:hover,
    .header-user-avatar:hover,
    #userAvatar:hover {
      transform: scale(1.1) !important;
    }
    
    /* ===== 其他样式 ===== */
    body {
      font-family: "Microsoft YaHei", sans-serif;
      background-color: #f5f5f5;
      margin: 0;
      padding: 0;
    }

    .header {
      background-color: #1976d2;
      color: white;
      padding: 10px 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
      position: fixed;
      width: 100%;
      top: 0;
      z-index: 1000;
    }
    
    .header-left {
      display: flex;
      align-items: center;
      gap: 20px;
    }

    .header h2 {
      margin: 0;
      font-size: 1.5rem;
      display: flex;
      align-items: center;
    }

    .header h2 img {
      height: 36px;
      margin-right: 10px;
    }

    .header-logo {
      width: 36px;
      height: 36px;
      margin-right: 12px;
    }

    .header-title {
      font-size: 1.4rem;
      font-weight: bold;
      letter-spacing: 1px;
    }

    .header-nav {
      margin-left: 32px;
      font-size: 1rem;
    }

    .header-nav a {
      color: #fff;
      text-decoration: none;
      margin-right: 16px;
      opacity: 0.85;
    }

    .header-nav a.active, .header-nav a:hover {
      opacity: 1;
      font-weight: bold;
    }

    .header .breadcrumb {
      margin: 0;
      background: transparent;
      color: white;
    }

    .header .breadcrumb a {
      color: rgba(255,255,255,0.8);
      text-decoration: none;
    }

    .header .breadcrumb a:hover {
      color: white;
    }

    .header .breadcrumb-item.active {
      color: white;
    }

    .header .breadcrumb-item+.breadcrumb-item::before {
      color: rgba(255,255,255,0.6);
    }

    /* 侧边栏样式 */
    .sidebar {
      position: fixed;
      top: 56px;
      left: 0;
      bottom: 0;
      width: 250px;
      background-color: white;
      box-shadow: 2px 0 5px rgba(0,0,0,0.1);
      overflow-y: auto;
      z-index: 100;
      padding-top: 20px;
    }

    .sidebar-menu {
      list-style: none;
      padding: 0;
      margin: 0;
    }

    .sidebar-menu > li {
      margin-bottom: 5px;
    }

    .sidebar-menu > li > a {
      display: flex;
      align-items: center;
      padding: 12px 20px;
      color: #333;
      text-decoration: none;
      transition: all 0.3s;
      border-left: 3px solid transparent;
    }

    .sidebar-menu > li > a:hover {
      background-color: #f0f0f0;
      border-left-color: #1976d2;
    }

    .sidebar-menu > li > a.active {
      background-color: #e3f2fd;
      border-left-color: #1976d2;
      color: #1976d2;
    }

    .sidebar-menu > li > a i {
      margin-right: 10px;
      width: 20px;
      text-align: center;
      font-size: 1.1rem;
    }

    .sidebar-submenu {
      list-style: none;
      padding-left: 53px;
      max-height: 0;
      overflow: hidden;
      transition: max-height 0.3s ease;
    }

    .sidebar-menu > li.open .sidebar-submenu {
      max-height: 500px;
    }

    .sidebar-submenu > li > a {
      display: block;
      padding: 8px 0;
      color: #666;
      text-decoration: none;
      transition: all 0.3s;
    }

    .sidebar-submenu > li > a:hover {
      color: #1976d2;
    }

    .sidebar-submenu > li > a.active {
      color: #1976d2;
      font-weight: 500;
    }

    /* 内容区样式 */
    .content {
      margin-left: 250px;
      padding: 76px 20px 20px;
    }
    
    /* 用户下拉菜单样式 */
    .header-user {
      display: flex;
      align-items: center;
      position: relative;
      cursor: pointer;
      padding: 8px 12px;
      border-radius: 8px;
      transition: all 0.2s ease;
    }
    .header-user:hover {
      background-color: rgba(255,255,255,0.1);
    }
    .header-user-name {
      font-size: 1rem;
      font-weight: 500;
      cursor: pointer;
      font-family: 'Microsoft YaHei', Arial, sans-serif;
      color: white;
    }
    .user-dropdown {
      position: absolute;
      top: 100%;
      right: 0;
      background: #fff;
      border-radius: 8px;
      box-shadow: 0 4px 12px rgba(0,0,0,0.15);
      min-width: 160px;
      z-index: 1001;
      opacity: 0;
      visibility: hidden;
      transform: translateY(-10px);
      transition: all 0.3s ease;
      margin-top: 8px;
    }
    .user-dropdown.show {
      opacity: 1;
      visibility: visible;
      transform: translateY(0);
    }
    .user-dropdown::before {
      content: '';
      position: absolute;
      top: -6px;
      right: 20px;
      width: 12px;
      height: 12px;
      background: #fff;
      transform: rotate(45deg);
      box-shadow: -2px -2px 4px rgba(0,0,0,0.1);
    }
    .user-dropdown-item {
      display: flex;
      align-items: center;
      padding: 12px 16px;
      color: #333;
      text-decoration: none;
      transition: background 0.2s;
      border-bottom: 1px solid #f0f0f0;
      font-family: 'Microsoft YaHei', Arial, sans-serif;
    }
    .user-dropdown-item:last-child {
      border-bottom: none;
    }
    .user-dropdown-item:hover {
      background: #f8f9fa;
      color: #2196f3;
    }
    .user-dropdown-item i {
      margin-right: 8px;
      width: 16px;
      text-align: center;
    }

    /* 班级管理相关样式 */
    .card {
      background: #fff;
      border-radius: 12px;
      box-shadow: 0 2px 12px rgba(33,150,243,0.07);
      margin-bottom: 24px;
      border: none;
    }

    .card-body {
      padding: 24px;
    }

    .search-bar {
      display: flex;
      align-items: center;
      gap: 12px;
      margin-bottom: 18px;
    }

    .search-bar input {
      border-radius: 8px;
      border: 1px solid #e3e3e3;
      padding: 6px 14px;
      font-size: 1rem;
      width: 220px;
    }

    .search-bar .btn {
      border-radius: 8px;
      font-size: 1rem;
      padding: 6px 18px;
      font-weight: 500;
      box-shadow: none;
    }

    .btn-primary {
      background: #2196f3;
      border: none;
    }

    .btn-primary:hover {
      background: #1976d2;
    }

    .btn-warning {
      background: #ffb300;
      color: #fff;
      border: none;
    }

    .btn-warning:hover {
      background: #ff9800;
      color: #fff;
    }

    .btn-success {
      background: #43a047;
      border: none;
    }

    .btn-success:hover {
      background: #388e3c;
    }

    .btn-danger {
      background: #fff0f0;
      color: #e53935;
      border: 1px solid #ffcdd2;
    }

    .btn-danger:hover {
      background: #ffcdd2;
      color: #b71c1c;
    }

    .table {
      background: #fff;
      border-radius: 10px;
      overflow: hidden;
      box-shadow: 0 2px 8px rgba(33,150,243,0.04);
    }

    .table thead th {
      background: #f5f7fa;
      color: #2196f3;
      font-weight: bold;
      text-align: center;
      border-bottom: 2px solid #e3e3e3;
    }

    .table tbody tr {
      transition: background 0.2s;
    }

    .table tbody tr:hover {
      background: #f8f9fa;
    }

    .table th, .table td {
      border: none;
      padding: 12px 16px;
      vertical-align: middle;
    }

    .table th {
      font-weight: 600;
      font-size: 0.95rem;
    }

    .table td {
      font-size: 0.95rem;
    }

    .pagination {
      justify-content: flex-start;
      margin-top: 12px;
    }

    .pagination .page-item .page-link {
      border: none;
      color: #2196f3;
      background: #fff;
      margin: 0 2px;
      border-radius: 6px;
      padding: 8px 12px;
      font-size: 0.95rem;
    }

    .pagination .page-item.active .page-link {
      background: #2196f3;
      color: #fff;
    }

    .pagination .page-link:hover {
      background: #e3f2fd;
      color: #1976d2;
    }

    .pagination .page-item.disabled .page-link {
      color: #ccc;
      background: #f8f9fa;
    }

    .modal-content {
      border-radius: 12px;
      border: none;
      box-shadow: 0 8px 32px rgba(33,150,243,0.15);
    }

    .modal-header {
      background: #f8f9fa;
      border-bottom: 1px solid #e9ecef;
      border-radius: 12px 12px 0 0;
    }

    .modal-title {
      color: #2196f3;
      font-weight: 600;
    }

    .form-label {
      font-weight: 500;
      color: #333;
      margin-bottom: 6px;
    }

    .form-control, .form-select {
      border: 1px solid #e3e3e3;
      border-radius: 8px;
      padding: 8px 12px;
      font-size: 1rem;
      transition: border-color 0.2s, box-shadow 0.2s;
    }

    .form-control:focus, .form-select:focus {
      border-color: #2196f3;
      box-shadow: 0 0 0 0.2rem rgba(33,150,243,0.25);
    }

    .alert {
      border-radius: 8px;
      border: none;
      font-size: 0.95rem;
    }

    .alert-success {
      background: #e8f5e8;
      color: #2e7d32;
    }

    .alert-danger {
      background: #ffebee;
      color: #c62828;
    }

    .alert-warning {
      background: #fff8e1;
      color: #ef6c00;
    }

    .alert-info {
      background: #e3f2fd;
      color: #1565c0;
    }

    /* 响应式设计 */
    @media (max-width: 768px) {
      .sidebar {
        transform: translateX(-100%);
        transition: transform 0.3s;
      }

      .content {
        margin-left: 0;
      }

      .search-bar {
        flex-wrap: wrap;
      }

      .search-bar input {
        min-width: 150px;
      }
    }
  </style>
</head>
<body>

<!-- 头部导航 -->
<div class="header">
  <div class="header-left">
    <img src="${pageContext.request.contextPath}/resources/images/logo.png" alt="Logo" class="header-logo">
    <span class="header-title">教务管理系统</span>
    <div class="header-nav">
      <a href="${pageContext.request.contextPath}/admin/home">首页</a>
      <span style="opacity:0.5;">/</span>
      <a href="#" class="active">班级信息</a>
    </div>
  </div>

  <!-- 用户名下拉菜单 -->
  <div class="header-user" id="userDropdownContainer">
    <img src="${pageContext.request.contextPath}/resources/images/baimao.png" 
         class="header-user-avatar" 
         alt="用户名头像" 
         id="userAvatar"
         style="width: 32px !important; height: 32px !important; max-width: 32px !important; max-height: 32px !important; min-width: 32px !important; min-height: 32px !important; border-radius: 50% !important; margin-right: 8px !important; object-fit: cover !important; background: #fff !important; cursor: pointer !important; transition: transform 0.2s !important; border: 2px solid rgba(255,255,255,0.3) !important; display: inline-block !important; vertical-align: middle !important; box-sizing: border-box !important;">
    <span class="header-user-name" id="userName">
      <c:choose>
        <c:when test="${not empty sessionScope.loggedInUser and not empty sessionScope.loggedInUser.realName}">
          ${sessionScope.loggedInUser.realName}
        </c:when>
        <c:when test="${not empty sessionScope.loggedInUser and not empty sessionScope.loggedInUser.username}">
          ${sessionScope.loggedInUser.username}
        </c:when>
        <c:otherwise>
          系统管理员
        </c:otherwise>
      </c:choose>
    </span>
    <div class="user-dropdown" id="userDropdown">
      <a href="#" class="user-dropdown-item" id="profileBtn">
        <i class="bi bi-person-circle"></i>
        个人中心
      </a>
      <a href="${pageContext.request.contextPath}/user/logout" class="user-dropdown-item">
        <i class="bi bi-box-arrow-right"></i>
        退出系统
      </a>
    </div>
  </div>
</div>

<!-- 侧边栏 -->
<div class="sidebar">
  <ul class="sidebar-menu">
    <li>
      <a href="${pageContext.request.contextPath}/admin/home">
        <i class="bi bi-house-door"></i> 系统首页
      </a>
    </li>
    <li>
      <a href="#" class="has-submenu">
        <i class="bi bi-megaphone"></i> 信息公告
      </a>
      <ul class="sidebar-submenu">
        <li><a href="${pageContext.request.contextPath}/admin/notifications/academic">教学通知</a></li>
        <li><a href="${pageContext.request.contextPath}/admin/notifications/exam">考试安排</a></li>
        <li><a href="${pageContext.request.contextPath}/admin/notifications/classroom">教室安排</a></li>
      </ul>
    </li>
    <li>
      <a href="#" class="has-submenu">
        <i class="bi bi-building"></i> 行政管理
      </a>
      <ul class="sidebar-submenu">
        <li><a href="${pageContext.request.contextPath}/admin/college">学院信息</a></li>
        <li><a href="${pageContext.request.contextPath}/admin/major">专业信息</a></li>
        <li><a href="${pageContext.request.contextPath}/admin/class" class="active">班级信息</a></li>
      </ul>
    </li>
    <li>
      <a href="#" class="has-submenu">
        <i class="bi bi-book"></i> 教学管理
      </a>
      <ul class="sidebar-submenu">
        <li><a href="${pageContext.request.contextPath}/admin/course">课程信息</a></li>
        <li><a href="${pageContext.request.contextPath}/admin/course-selection">选课管理</a></li>
        <li><a href="${pageContext.request.contextPath}/admin/score">成绩管理</a></li>
        <li><a href="${pageContext.request.contextPath}/admin/online">网上评教</a></li>
      </ul>
    </li>
    <li>
      <a href="#" class="has-submenu">
        <i class="bi bi-calendar3"></i> 教务管理
      </a>
      <ul class="sidebar-submenu">
        <li><a href="${pageContext.request.contextPath}/admin/leave">请假管理</a></li>
        <li><a href="${pageContext.request.contextPath}/admin/homework">作业提交</a></li>
        <li><a href="${pageContext.request.contextPath}/admin/attendance">考勤信息</a></li>
        <li><a href="${pageContext.request.contextPath}/admin/online">网上评教</a></li>
      </ul>
    </li>
    <li>
      <a href="#" class="has-submenu">
        <i class="bi bi-person"></i> 用户管理
      </a>
      <ul class="sidebar-submenu">
        <li><a href="${pageContext.request.contextPath}/admin/admin">管理员信息</a></li>
        <li><a href="${pageContext.request.contextPath}/admin/teacher">教师信息</a></li>
        <li><a href="${pageContext.request.contextPath}/admin/student">学生信息</a></li>
      </ul>
    </li>
    <li>
      <a href="${pageContext.request.contextPath}/user/logout">
        <i class="bi bi-box-arrow-right"></i> 退出系统
      </a>
    </li>
  </ul>
</div>

<!-- 主要内容区域 -->
<div class="content">
  <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">班级信息管理</h1>
  </div>

  <div class="card">
    <div class="card-body">
      <!-- 搜索表单 -->
      <form class="search-bar" method="get" action="${pageContext.request.contextPath}/admin/class">
        <input type="text" class="form-control" name="nameSearch" value="${param.nameSearch}" placeholder="请输入班级名称查询">
        <button type="submit" class="btn btn-primary">查询</button>
        <a href="${pageContext.request.contextPath}/admin/class" class="btn btn-warning">重置</a>
      </form>

      <!-- 操作按钮 -->
      <div class="mb-3" style="display:flex;gap:12px;">
        <button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#classModal" id="addBtn">
          新增
        </button>
        <button type="button" class="btn btn-danger" id="batchDeleteBtn">
          批量删除
        </button>
      </div>

      <!-- 班级信息列表 -->
      <div class="table-responsive">
        <table class="table table-hover">
          <thead>
          <tr>
            <th style="width: 5%;"><input type="checkbox" id="checkAll"></th>
            <th style="width: 10%;">编号</th>
            <th style="width: 25%;">班级名称</th>
            <th style="width: 20%;">年级</th>
            <th style="width: 25%;">所属专业</th>
            <th style="width: 15%;">操作</th>
          </tr>
          </thead>
          <tbody id="classTableBody">
          <c:if test="${empty classes}">
            <tr>
              <td colspan="6" class="text-center">暂无班级信息</td>
            </tr>
          </c:if>
          <c:forEach var="classObj" items="${classes}" varStatus="status">
            <tr>
              <td><input type="checkbox" class="class-checkbox" value="${classObj.id}"></td>
              <td>${status.index + 1}</td>
              <td>${classObj.name}</td>
              <td>${classObj.grade}</td>
              <td>
                <c:choose>
                  <c:when test="${not empty classObj.major}">
                    ${classObj.major.name}
                  </c:when>
                  <c:otherwise>
                    N/A
                  </c:otherwise>
                </c:choose>
              </td>
              <td>
                <button class="btn btn-sm btn-warning edit-btn" data-id="${classObj.id}" data-bs-toggle="modal" data-bs-target="#classModal">编辑</button>
                <a href="${pageContext.request.contextPath}/admin/class/delete/${classObj.id}" class="btn btn-sm btn-danger" onclick="return confirm('确定删除该班级吗？');">删除</a>
              </td>
            </tr>
          </c:forEach>
          </tbody>
        </table>
      </div>

      <!-- 分页 -->
      <nav aria-label="Page navigation">
        <ul class="pagination justify-content-center">
          <c:if test="${not empty currentPage and currentPage > 1}">
            <li class="page-item">
              <a class="page-link" href="?page=${currentPage - 1}&size=${pageSize}&nameSearch=${nameSearch}" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
              </a>
            </li>
          </c:if>

          <c:if test="${not empty totalPages}">
            <c:forEach begin="1" end="${totalPages}" var="i">
              <li class="page-item ${currentPage == i ? 'active' : ''}">
                <a class="page-link" href="?page=${i}&size=${pageSize}&nameSearch=${nameSearch}">${i}</a>
              </li>
            </c:forEach>
          </c:if>

          <c:if test="${not empty currentPage and not empty totalPages and currentPage < totalPages}">
            <li class="page-item">
              <a class="page-link" href="?page=${currentPage + 1}&size=${pageSize}&nameSearch=${nameSearch}" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
              </a>
            </li>
          </c:if>
        </ul>
        <div class="text-left mt-2" style="font-size:0.98rem;color:#888;">
          共 ${totalRecords} 条记录
        </div>
      </nav>
    </div>
  </div>
</div>

<!-- 新增/编辑班级信息模态框 -->
<div class="modal fade" id="classModal" tabindex="-1" aria-labelledby="classModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="classModalLabel">新增班级</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <form id="classForm" action="${pageContext.request.contextPath}/admin/class/add" method="post">
          <input type="hidden" id="classId" name="id">
          <div class="mb-3">
            <label for="className" class="form-label">班级名称</label>
            <input type="text" class="form-control" id="className" name="name" required>
          </div>
          <div class="mb-3">
            <label for="classGrade" class="form-label">年级</label>
            <input type="number" class="form-control" id="classGrade" name="grade" required>
          </div>
          <div class="mb-3">
            <label for="majorSelect" class="form-label">所属专业</label>
            <select class="form-select" id="majorSelect" name="major.id" required>
              <option value="">请选择专业</option>
              <c:forEach var="major" items="${majorsList}">
                <option value="${major.id}">${major.name}</option>
              </c:forEach>
            </select>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" id="submitClassFormBtn">保存</button>
      </div>
    </div>
  </div>
</div>

<!-- 个人中心模态框 -->
<div class="modal fade" id="profileModal" tabindex="-1" aria-labelledby="profileModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="profileModalLabel">个人中心</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <ul class="nav nav-tabs" id="profileTabs" role="tablist">
          <li class="nav-item">
            <button class="nav-link active" id="profile-info-tab" data-bs-toggle="tab" data-bs-target="#profile-info" type="button" role="tab" aria-controls="profile-info" aria-selected="true">
              <i class="bi bi-person"></i> 个人信息
            </button>
          </li>
          <li class="nav-item">
            <button class="nav-link" id="change-password-tab" data-bs-toggle="tab" data-bs-target="#change-password" type="button" role="tab" aria-controls="change-password" aria-selected="false">
              <i class="bi bi-key"></i> 修改密码
            </button>
          </li>
        </ul>
        <div class="tab-content" id="profileTabContent">
          <div class="tab-pane fade show active" id="profile-info" role="tabpanel" aria-labelledby="profile-info-tab">
            <form id="profileForm" class="mt-3">
              <div class="row">
                <div class="col-md-6">
                  <div class="mb-3">
                    <label for="profileUsername" class="form-label">用户名</label>
                    <input type="text" class="form-control" id="profileUsername" readonly>
                  </div>
                </div>
                <div class="col-md-6">
                  <div class="mb-3">
                    <label for="profileRole" class="form-label">角色</label>
                    <input type="text" class="form-control" id="profileRole" readonly>
                  </div>
                </div>
              </div>
              <div class="mb-3">
                <label for="profileRealName" class="form-label">真实姓名</label>
                <input type="text" class="form-control" id="profileRealName" required>
              </div>
              <div class="d-grid gap-2">
                <button type="button" class="btn btn-primary" id="updateProfileBtn">
                  <i class="bi bi-check-circle"></i> 更新信息
                </button>
              </div>
            </form>
          </div>
          <div class="tab-pane fade" id="change-password" role="tabpanel" aria-labelledby="change-password-tab">
            <form id="passwordForm" class="mt-3">
              <div class="mb-3">
                <label for="oldPassword" class="form-label">原密码</label>
                <input type="password" class="form-control" id="oldPassword" required>
              </div>
              <div class="mb-3">
                <label for="newPassword" class="form-label">新密码</label>
                <input type="password" class="form-control" id="newPassword" required minlength="6">
                <div class="form-text">密码长度至少6位</div>
              </div>
              <div class="mb-3">
                <label for="confirmPassword" class="form-label">确认新密码</label>
                <input type="password" class="form-control" id="confirmPassword" required>
              </div>
              <div class="d-grid gap-2">
                <button type="button" class="btn btn-warning" id="changePasswordBtn">
                  <i class="bi bi-key"></i> 修改密码
                </button>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- jQuery -->
<script src="${pageContext.request.contextPath}/resources/bootstrap5/jquery-3.6.0.min.js"></script>
<!-- Bootstrap Bundle with Popper -->
<script src="${pageContext.request.contextPath}/resources/bootstrap5/js/bootstrap.bundle.min.js"></script>

<script>
  $(document).ready(function() {
    // 用户下拉菜单功能
    $('#userDropdownContainer').click(function(e) {
      e.stopPropagation();
      console.log('用户头像区域被点击了 - 管理员班级页面');
      const dropdown = $('#userDropdown');
      
      if (!dropdown.length) {
        console.error('找不到下拉菜单元素');
        return;
      }
      
      // 使用更准确的状态检测
      const computedStyle = window.getComputedStyle(dropdown[0]);
      const isReallyVisible = computedStyle.opacity !== '0' && computedStyle.visibility !== 'hidden' && dropdown[0].offsetHeight > 0;
      
      console.log('CSS opacity:', computedStyle.opacity);
      console.log('CSS visibility:', computedStyle.visibility);
      console.log('元素高度:', dropdown[0].offsetHeight);
      console.log('真实显示状态:', isReallyVisible);
      
      if (isReallyVisible) {
        // 隐藏
        dropdown.css({
          'opacity': '0',
          'visibility': 'hidden',
          'transform': 'translateY(-10px)'
        }).removeClass('show');
        console.log('隐藏下拉菜单');
      } else {
        // 显示
        dropdown.css({
          'opacity': '1',
          'visibility': 'visible',
          'transform': 'translateY(0)'
        }).addClass('show');
        console.log('显示下拉菜单');
      }
    });

    // 点击其他地方关闭下拉菜单
    $(document).click(function(e) {
      if (!$(e.target).closest('#userDropdownContainer').length) {
        $('#userDropdown').css({
          'opacity': '0',
          'visibility': 'hidden',
          'transform': 'translateY(-10px)'
        }).removeClass('show');
      }
    });

    // 个人中心功能
    $('#profileBtn').click(function(e) {
      e.preventDefault();
      $('#userDropdown').removeClass('show');
      loadUserProfile();
      $('#profileModal').modal('show');
    });

    // 加载用户个人信息
    function loadUserProfile() {
      $.ajax({
        url: '${pageContext.request.contextPath}/user/profile',
        type: 'GET',
        success: function(response) {
          if (response.success) {
            const user = response.user;
            $('#profileUsername').val(user.username);
            $('#profileRealName').val(user.realName || '');

            // 设置角色显示
            let roleText = '';
            switch(user.role) {
              case 1: roleText = '管理员'; break;
              case 2: roleText = '教师'; break;
              case 3: roleText = '学生'; break;
              default: roleText = '未知'; break;
            }
            $('#profileRole').val(roleText);
          } else {
            alert('获取用户个人信息失败: ' + response.message);
          }
        },
        error: function() {
          alert('获取用户个人信息失败，请重试');
        }
      });
    }

    // 更新个人信息
    $('#updateProfileBtn').click(function() {
      const realName = $('#profileRealName').val().trim();
      if (!realName) {
        alert('请输入真实姓名');
        return;
      }

      $.ajax({
        url: '${pageContext.request.contextPath}/user/updateProfile',
        type: 'POST',
        data: {
          realName: realName
        },
        success: function(response) {
          if (response.success) {
            alert('个人信息更新成功');
            // 更新页面显示的用户名
            $('#userName').text(realName);
            $('#profileModal').modal('hide');
            // 刷新页面以更新所有显示的用户名
            setTimeout(function() {
              location.reload();
            }, 1000);
          } else {
            alert('更新失败: ' + response.message);
          }
        },
        error: function() {
          alert('更新失败，请重试');
        }
      });
    });

    // 修改密码
    $('#changePasswordBtn').click(function() {
      const oldPassword = $('#oldPassword').val();
      const newPassword = $('#newPassword').val();
      const confirmPassword = $('#confirmPassword').val();

      if (!oldPassword || !newPassword || !confirmPassword) {
        alert('请输入完整的密码信息');
        return;
      }

      if (newPassword.length < 6) {
        alert('新密码长度不能少于6位');
        return;
      }

      if (newPassword !== confirmPassword) {
        alert('新密码和确认密码不一致');
        return;
      }

      $.ajax({
        url: '${pageContext.request.contextPath}/user/changePassword',
        type: 'POST',
        data: {
          oldPassword: oldPassword,
          newPassword: newPassword,
          confirmPassword: confirmPassword
        },
        success: function(response) {
          if (response.success) {
            alert('密码修改成功');
            $('#passwordForm')[0].reset();
            $('#profileModal').modal('hide');
          } else {
            alert('修改失败: ' + response.message);
          }
        },
        error: function() {
          alert('修改失败，请重试');
        }
      });
    });

    // 模态框关闭时重置表单
    $('#profileModal').on('hidden.bs.modal', function() {
      $('#profileForm')[0].reset();
      $('#passwordForm')[0].reset();
      // 切换回个人信息标签
      $('#profile-info-tab').tab('show');
    });

    // 新增按钮点击事件
    $('#addBtn').on('click', function() {
      $('#classModalLabel').text('新增班级');
      $('#classForm')[0].reset();
      $('#classId').val('');
      $('#classForm').attr('action', '${pageContext.request.contextPath}/admin/class/add');
    });

    // 编辑按钮点击事件
    $('#classTableBody').on('click', '.edit-btn', function() {
      $('#classModalLabel').text('编辑班级');
      $('#classForm')[0].reset();
      const classId = $(this).data('id');
      $('#classId').val(classId);
      $('#classForm').attr('action', '${pageContext.request.contextPath}/admin/class/edit');

      $.ajax({
        url: `${pageContext.request.contextPath}/admin/class/view/`+classId,
        method: 'GET',
        success: function(classObj) {
          if (classObj) {
            $('#className').val(classObj.name);
            $('#classGrade').val(classObj.grade);
            if (classObj.major && classObj.major.id) {
              $('#majorSelect').val(classObj.major.id);
            } else {
              $('#majorSelect').val('');
            }
          } else {
            alert('加载班级详情失败');
            $('#classModal').modal('hide');
          }
        },
        error: function() {
          alert('加载班级详情失败');
          $('#classModal').modal('hide');
        }
      });
    });

    // 保存班级信息按钮点击事件
    $('#submitClassFormBtn').on('click', function() {
      if ($('#classForm')[0].checkValidity() === false) {
        event.preventDefault();
        event.stopPropagation();
      }
      $('#classForm').addClass('was-validated');

      if ($('#classForm')[0].checkValidity() === true) {
        $('#classForm').submit();
      }
    });

    // 批量删除按钮点击事件
    $('#batchDeleteBtn').on('click', function() {
      const selectedIds = Array.from(document.querySelectorAll('.class-checkbox:checked'))
              .map(checkbox => checkbox.value)
              .join(',');

      if (!selectedIds) {
        alert('请选择要删除的班级');
        return;
      }

      if (confirm('确定批量删除选中的班级吗？')) {
        window.location.href = "${pageContext.request.contextPath}/admin/class/batchDelete?ids=" + selectedIds;
      }
    });

    // 全选/取消全选逻辑
    $('#checkAll').on('change', function() {
      $('.class-checkbox').prop('checked', this.checked);
    });

    // 侧边栏子菜单展开/收起功能
    $('.has-submenu').click(function(e) {
      e.preventDefault();
      const parent = $(this).parent();
      const submenu = parent.find('.sidebar-submenu');
      
      if (parent.hasClass('open')) {
        parent.removeClass('open');
        submenu.css('max-height', '0');
      } else {
        $('.sidebar-menu > li.open').removeClass('open').find('.sidebar-submenu').css('max-height', '0');
        parent.addClass('open');
        submenu.css('max-height', '500px');
      }
    });
  });
</script>
</body>
</html>

